import InteractiveBase from "../../../LevelCommon/InteractiveBase";
import MoveLock from "./MoveLock";
import Sound from "../../../../constant/SoundNameConstant";
import { utils } from "../../../../utils/CommonUtil";

export default class MoveLockKey extends InteractiveBase {
    /** 材质*/
     private curMaterial:Laya.BlinnPhongMaterial;
    /** 材质初始颜色*/
     private originColor:Laya.Vector4;
    /** 锁*/
    private moveLock: MoveLock;
    /** 当前的位置索引*/
    public curPosIndex: number;

    private mowner: Laya.MeshSprite3D;
    //是否转换unity坐标到laya
    public isConvertUnityPosition: boolean = false;

    private childAtParentIndex = 0;

    onStart() {
        this.mowner = this.owner as Laya.MeshSprite3D;
        this.curMaterial = this.mowner.meshRenderer.material as Laya.BlinnPhongMaterial;
         this.originColor = this.curMaterial.albedoColor;
        this.moveLock = this.mowner.parent.getComponent(MoveLock);
        this.childAtParentIndex = utils.CommonUtils.GetChildComponentIndexInParent(this.moveLock.owner, this.owner.name, MoveLockKey);
    }
    protected InteractiveLogic(): boolean {
        /** 如果正在播动画 或者已经对了 就不操作*/
        if (this.moveLock.isCorrect || this.moveLock.IsPlayingTween()) {
            return false;
        }
        /** 当前选中的物体*/
        let curSelect: MoveLockKey = this.moveLock.curSelectKey;
        /** 点击了物体*/
        if (curSelect == null) {
            this.moveLock.SelectKey(this);
            this.SetSelect(true);
        }
        else {
            /** 如果选的是自己 取消选中*/
            if (curSelect == this) {
                this.moveLock.SelectKey(null);
                this.SetSelect(false);
            }
            else {
                /** 选中了别的  和他交换*/
                window.gameFacade.SoundMng.playSound(Sound.soundClipArr.MOVE);
                this.moveLock.ChangeKey(this);
            }
        }
        return false;
    }
    /** 设置是否选中物体*/
    public SetSelect(isSelect: boolean) {
        if (isSelect) {
            this.curMaterial.albedoColor = new Laya.Vector4(1,0,0,1);
        }
        else {
             this.curMaterial.albedoColor = this.originColor;
        }
    }
    /** 设置移动位置*/
    public DoMovePos(pos: Laya.Transform3D, index: number, completeHandler: Laya.Handler) {
        this.curPosIndex = index;
        this.moveLock.ChildMoveLockKeysCurIndex[this.childAtParentIndex] = this.curPosIndex;
        utils.CommonUtils.DOPositionTween(
            this.mowner.transform,
            this.mowner.transform.position,
            pos.position, 500,
            Laya.Handler.create(this, function () {
                console.log("移动到了目标位置了", pos.position);
                if(completeHandler){
                    completeHandler.run();
                }
                
            }),false,false,this.isConvertUnityPosition)
    }
}